package com.health.utils;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.health.properties.WeChatProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;


@Slf4j
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PayUtil {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WeChatProperties weChatProperties;

    public String GetSubstring(Integer money, Long out_trade_no, String openId) throws Exception {
        HashMap<String, Object> paramMap = new HashMap<>();
        HashMap<String, Object> amount = new HashMap<>();
        amount.put("total", money);
        amount.put("currency", "CNY");
        paramMap.put("amount", amount);
        paramMap.put("mchid", weChatProperties.getMchid());
        paramMap.put("description", "充值");
        paramMap.put("notify_url", "http://localhost:8080/pay/callback");
        HashMap<String, Object> payer = new HashMap<>();
        payer.put("openId", openId);
        paramMap.put("payer", payer);
        paramMap.put("out_trade_no", out_trade_no);
        paramMap.put("appid", weChatProperties.getAppid());

        log.info("paramMap :{}" ,paramMap.toString());
        String result = restTemplate.postForObject("http://localhost:9090/v3/pay/transactions/jsapi", paramMap, String.class);

        JSONObject jsonObject = JSON.parseObject(result);
        String prepayId = jsonObject.getString("prepay_id");
        log.info("======prepayId:{}", prepayId);

        return prepayId;
    }

}
